package com.boco.transnms.server.dao.traph;

import com.boco.transnms.server.dao.base.GenericDAO;
import com.boco.transnms.common.dto.base.DboCollection;
import com.boco.transnms.common.dto.base.BoQueryContext;
import com.boco.transnms.common.dto.QueryAlcatelLucentAttempDO;
import com.boco.transnms.server.dao.base.DaoHelper;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.server.dao.base.DaoName;
import com.boco.transnms.common.dto.AlcatellucentAttemp;
import com.boco.transnms.common.dto.misc.AlcatelLucentAttach;
import com.boco.transnms.common.dto.base.*;
import com.boco.common.util.db.SqlHelper;
import com.boco.common.util.db.DbConnManager;
import com.boco.common.util.lang.TimeFormatHelper;

public class AlcatelLucentDAO extends GenericDAO{
    public AlcatelLucentDAO() {
        super(DaoName.AlcatelLucentDAO);
    }

    public String addAlcatelLucentAttemp(BoActionContext actionContext, AlcatellucentAttemp alcatelLucentAttemp) throws Exception {
        super.createObject(actionContext, alcatelLucentAttemp);
        return alcatelLucentAttemp.getCuid();
    }

    public String addAlcatelLucentAttach(BoActionContext actionContext, AlcatelLucentAttach alcatelLucentAttach) throws Exception {
        super.insertDbo(actionContext, alcatelLucentAttach);
        if(alcatelLucentAttach.getAttachData()!=null){
            String sql = AlcatelLucentAttach.AttrName.Cuid + " = '" + alcatelLucentAttach.getCuid() +  "'";
            super.updateBlob(actionContext, AlcatelLucentAttach.CLASS_NAME, AlcatelLucentAttach.AttrName.AttachData, alcatelLucentAttach.getAttachData(), sql);
        }
        return alcatelLucentAttach.getCuid();
    }

    public void addAlcatelLucentAttemps(BoActionContext actionContext, DataObjectList list) throws Exception {
        super.createObjects(actionContext,list);
    }

    public void addAlcatelLucentAttachs(BoActionContext actionContext, DataObjectList list) throws Exception {
        super.insertDbos(actionContext,list);
        for(GenericDO dbo : list){
           AlcatelLucentAttach attach = (AlcatelLucentAttach) dbo;
           if(attach.getAttachData()!=null){
               String sql = AlcatelLucentAttach.AttrName.Cuid + " = '" + attach.getCuid() +  "'";
               super.updateBlob(actionContext, AlcatelLucentAttach.CLASS_NAME, AlcatelLucentAttach.AttrName.AttachData, attach.getAttachData(), sql);
           }
       }
    }

    public DboCollection getAlcatelLucentAttachByAttempCuid(BoQueryContext boQueryContext, String cuid) throws Exception {
        if(DaoHelper.isNotEmpty(cuid)){
            String sql = "SELECT * FROM ALCATELLUCENT_ATTACH WHERE RELATED_AL_ATTEMP_CUID='" + cuid + "'";
            return super.selectDBOs(boQueryContext, sql, new AlcatelLucentAttach());
        }else{
            return null;
        }
    }

    public AlcatelLucentAttach getAlcatelLucentAttachByAttempCuidAndFileName(BoQueryContext boQueryContext, String cuid, String name) throws Exception {
        if(DaoHelper.isNotEmpty(cuid)&&DaoHelper.isNotEmpty(name)){
            String sql = "SELECT * FROM ALCATELLUCENT_ATTACH WHERE RELATED_AL_ATTEMP_CUID='" + cuid + "' AND " +
                         AlcatelLucentAttach.AttrName.AttachFileName + "='" + name + "'";
            DboCollection dboc = super.selectDBOs(boQueryContext, sql, new AlcatelLucentAttach());
            if(null != dboc && dboc.size() == 1){
                return (AlcatelLucentAttach)dboc.getAttrField(AlcatelLucentAttach.CLASS_NAME,0);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    public DboCollection getAttempByPage(BoQueryContext boQueryContext, QueryAlcatelLucentAttempDO queryAlcatelLucentAttempDO) throws Exception {
        StringBuffer sql = new StringBuffer("SELECT * FROM ALCATELLUCENT_ATTEMP WHERE 1=1");
        if(DaoHelper.isNotEmpty(queryAlcatelLucentAttempDO.getHeadingNum())){
            sql.append(" AND " + AlcatellucentAttemp.AttrName.headingNum + " LIKE '%" + queryAlcatelLucentAttempDO.getHeadingNum() + "%'");
        }
        if(DaoHelper.isNotEmpty(queryAlcatelLucentAttempDO.getRelatedDesignerisCuid())){
            sql.append(" AND " + AlcatellucentAttemp.AttrName.relatedDesignerisCuid + " LIKE '%" + queryAlcatelLucentAttempDO.getRelatedDesignerisCuid() + "%'");
        }
        if(DaoHelper.isNotEmpty(queryAlcatelLucentAttempDO.getTitle())){
            sql.append(" AND " + AlcatellucentAttemp.AttrName.title + " LIKE '%" + queryAlcatelLucentAttempDO.getTitle() + "%'");
        }
        if(DaoHelper.isNotEmpty(queryAlcatelLucentAttempDO.getUrgent())){
            if(!queryAlcatelLucentAttempDO.getUrgent().equals("0")){
                sql.append(" AND " + AlcatellucentAttemp.AttrName.urgent + " = " + queryAlcatelLucentAttempDO.getUrgent());
            }
        }

        String createtime = queryAlcatelLucentAttempDO.getStartDate();
        if(DaoHelper.isNotEmpty(createtime)){
            createtime = SqlHelper.getTimestamp(DbConnManager.getInstance().getDbContext().getDbType(),
                    TimeFormatHelper.getFormatTimestamp(createtime));
            sql.append(" AND " + AlcatellucentAttemp.AttrName.usedDate + ">=" + createtime);
        }

        String endtime = queryAlcatelLucentAttempDO.getEndDate();
        if(DaoHelper.isNotEmpty(endtime)){
            endtime = SqlHelper.getTimestamp(DbConnManager.getInstance().getDbContext().getDbType(),
                    TimeFormatHelper.getFormatTimestamp(endtime));
            sql.append(" AND " + AlcatellucentAttemp.AttrName.usedDate + "<=" + endtime);
        }
        sql.append(" ORDER BY " + queryAlcatelLucentAttempDO.getOrderFieldString());
        return super.selectDBOs(boQueryContext, sql.toString(), new AlcatellucentAttemp());
    }
}
